//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package 力扣.题库.未分类.demo20AND40;

import java.util.Arrays;

public class demo31 {
    static int[] nums = new int[]{1, 2, 3};

    public demo31() {
    }

    public static void main(String[] args) {
        nextPermutation(nums);

        for(int i = 0; i < nums.length; ++i) {
            System.out.println(nums[i]);
        }

    }

    public static void nextPermutation(int[] nums) {
        boolean flag = true;

        for(int i = nums.length - 1; i >= 1; --i) {
            if (nums.length > 2 && i == 1 && nums[i - 1] < nums[i]) {
                int temp = nums[i];
                int index = 0;
                Arrays.sort(nums);

                for(int j = 0; j < nums.length; ++j) {
                    if (nums[j] == temp) {
                        index = j - 1;
                        break;
                    }
                }

                if (index == 0) {
                    return;
                }

                temp = nums[0];
                nums[0] = nums[index];
                nums[index] = temp;
                Arrays.sort(nums, 1, nums.length);
                return;
            }

            if (nums[i - 1] < nums[i]) {
                int temp = nums[i];
                nums[i] = nums[i - 1];
                nums[i - 1] = temp;
                flag = false;
                break;
            }
        }

        if (flag) {
            for(int i = 0; i < nums.length / 2; ++i) {
                int temp = nums[i];
                nums[i] = nums[nums.length - 1 - i];
                nums[nums.length - 1 - i] = temp;
            }
        }

    }
}
